package com.itheima.thread04;

import java.security.cert.CertificateParsingException;

//生产者
public class Cooker implements Runnable{
    private Desk desk;

    public  Cooker(){}

    public Cooker(Desk desk) {
        this.desk = desk;
    }

    @Override
    public void run() {
        while (true){
            synchronized (desk.getLock()){
                if (desk.getCount()==0){
                    break;
                }else{
                    if (!desk.isFlag()){
                        //没有汉堡，做汉堡
                        System.out.println("厨师在做汉堡");
                        //更改共享数据为true，表示已经有汉堡了
                        desk.setFlag(true);
                        //唤醒foodie
                        desk.getLock().notifyAll();
                    }else {
                        try {
                            //桌上还有汉堡，就不用做，进入等待
                            desk.getLock().wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
}
